/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.internal.parser;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;

/**
 * Detects the start and end of a partition in a JBehave scenario
 * 
 * @author Doug Satchwell
 */
public class JFunctionMultiLineRule extends MultiLineRule {
  
  public final char[][] chars;

  public JFunctionMultiLineRule(String[] keywords, String startSequence, IToken token) {
    super(startSequence, "", token, (char)0, true);
    chars = new char[keywords.length][];
    for (int i=0;i<keywords.length;i++) {
      String keyword = keywords[i];
      chars[i] = ("\n"+keyword).toCharArray();
    }
  }

  @Override
  protected boolean endSequenceDetected(ICharacterScanner scanner) {
    int c;
    while ((c = scanner.read()) != ICharacterScanner.EOF) {
      if (c == '\n') {        
        for (char[] charray : chars) {
          if (sequenceDetected(scanner, charray, false)) {
            // we found another of our partitions, so unread the start sequence and return true
            for (int i=0; i < charray.length-1; i++) {
              scanner.unread();
            }
            return true;
          }
        }
      }
    }
    return true;
  }  
}
